package com.ruoyi.web.controller.module.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customer.domain.CustomerCoupon;
import com.ruoyi.customer.service.ICustomerCouponService;
import com.ruoyi.market.domain.Coupon;
import com.ruoyi.market.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户卡券信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/customer/coupon")
public class CustomerCouponController extends BaseController
{
    @Autowired
    private ICustomerCouponService customerCouponService;

    /**
     * 获取客户卡券列表
     */
    @PostMapping("/customerCouponList")
    public TableDataInfo list(@RequestBody CustomerCoupon customerCoupon, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            customerCoupon.setShopId(shopId);
        }
        startPageByPost();
        Long userId = SecurityUtils.getUserId();
        customerCoupon.setCustomerId(userId);
        List<CustomerCoupon> list = customerCouponService.selectCustomerCouponList(customerCoupon);
        return getDataTable(list);
    }

    /**
     * 获取客户卡券列表 - 全部
     */
    @PostMapping("/customerCouponListAll")
    public TableDataInfo customerCouponListAll(@RequestBody CustomerCoupon customerCoupon, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            customerCoupon.setShopId(shopId);
        }
        startPageByPost();
        List<CustomerCoupon> list = customerCouponService.selectCustomerCouponList(customerCoupon);
        return getDataTable(list);
    }

    /**
     * 根据客户卡券编号获取详细信息
     */
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            CustomerCoupon sysCustomerCoupon = customerCouponService.selectCustomerCouponById(id);
            ajax.put(AjaxResult.DATA_TAG, sysCustomerCoupon);
        }
        return ajax;
    }

    /**
     * 新增客户卡券
     */
    @Log(title = "客户卡券管理 - 领取/新增", businessType = BusinessType.INSERT)
    @PostMapping("/customerCouponInsert")
    public AjaxResult add(@Validated @RequestBody CustomerCoupon customerCoupon, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            customerCoupon.setShopId(shopId);
        }
        customerCoupon.setCreateBy(getUsername());
        return toAjax(customerCouponService.insertCustomerCoupon(customerCoupon));
    }

    /**
     * 修改客户卡券
     */
    @Log(title = "客户卡券管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CustomerCoupon customerCoupon)
    {
        if (!customerCouponService.checkCustomerCouponUnique(customerCoupon))
        {
            return error("修改客户卡券'" + customerCoupon.getCouponTitle() + "'失败，已存在");
        }
        customerCoupon.setUpdateBy(getUsername());
        return toAjax(customerCouponService.updateCustomerCoupon(customerCoupon));
    }

    /**
     * 删除客户卡券
     */
    @Log(title = "客户卡券管理 - 删除客户卡券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerCouponService.deleteCustomerCouponByIds(ids));
    }

    /**
     * 使用状态修改
     */
    @Log(title = "客户卡券管理 - 使用状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/customerCouponChangeStatus")
    public AjaxResult changeStatus(@RequestBody CustomerCoupon customerCoupon)
    {
        customerCoupon.setUpdateBy(getUsername());
        return toAjax(customerCouponService.updateCustomerCouponStatus(customerCoupon));
    }

    /**
     * 激活状态修改
     */
    @Log(title = "客户卡券管理 - 激活状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/customerCouponChangeActive")
    public AjaxResult changeActive(@RequestBody CustomerCoupon customerCoupon)
    {
        customerCoupon.setUpdateBy(getUsername());
        return toAjax(customerCouponService.updateCustomerCouponActive(customerCoupon));
    }
}
